<template>
	<view>
		<view class="u-flex user-box u-p-l-30 u-p-r-20 u-p-b-30">
			<u-form :model="model" ref="uForm" style="width: 100%;">
				<u-form-item label-width="130" label="IMEI:">
					<u-input v-model="model.address" type="text" :disabled="true" />
					<u-icon slot="right" name="scan" @tap="scanCode"></u-icon>
				</u-form-item>
				<u-form-item label-width="130" label="名称:"><u-input v-model="model.address" type="text" /></u-form-item>
				<u-form-item label-width="120" label="所属组:">
					<u-select v-model="equipShow" :list="equipList" value-name="code" label-name="name" @confirm="confirm"></u-select>
					<u-tag :text="getTyp(model.equipId)" @click="equipShow = true" />
				</u-form-item>
				<u-form-item label-width="120" label="型号:">
					<u-select v-model="equipShow" :list="equipList" value-name="code" label-name="name" @confirm="confirm"></u-select>
					<u-tag :text="getTyp(model.equipId)" @click="equipShow = true" />
					<u-icon slot="right" name="scan" @tap="scanCode"></u-icon>
				</u-form-item>
			</u-form>
		</view>
		<u-gap height="20"></u-gap>
		<view class="btn-line">
			<button class="landing" type="primary" style="width: 90%;" @click="bindConfirm">{{btnTxt}}</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			equipShow: false,
			visible: false,
			equipList: [],
			model: {
				priority:1
			},
			type: [],
			detailData:null,
			btnTxt:'保存'
		};
	},
	onLoad(option) {
		this.detailData = option
		this.model.occurrence = this.$u.timeFrom();
		this.request({
			url: 'BaseEquipment/pages',
			data: {
				limit: 0
			},
			success: res => {
				this.type = res.data;
				this.equipList = res.data;
			}
		});
	},
	onShow() {
		this.initDetail();
	},
	methods: {
		initDetail(){
			console.log('==',this.detailData)
			this.btnTxt = this.detailData.from == 'edit' ? '保存' : '提交'
			uni.setNavigationBarTitle({
				title: this.detailData.from == 'edit' ? this.detailData.name : '新增设备'
			});
		},
		confirm(e) {
			if (e.length > 0) {
				this.model.equipId = e[0].value;
			}
		},
		onUploaded(lists, name) {
			this.model.urls = [];
			lists.forEach(m => {
				this.model.urls.push(m.response.data.id);
			});
		},
		onRemove(index, lists, name) {
			if (this.model.urls != undefined && this.model.urls.length>0){
				this.model.urls.forEach(m=>{
					if (!lists.some(n=>n.response.data.id == m)){
						this.request({
							url: 'files/delete?id='+m,
							method: 'POST',
							success: res => {
							}
						});
					}
				})
			}
			this.model.urls = [];
			lists.forEach(m => {
				this.model.urls.push(m.response.data.id);
			});
		},
		getTyp(id) {
			let typ = this.type.find(m => m.code == id);
			if (typ != undefined) {
				return typ.name;
			} else {
				return '请选择设备';
			}
		},
		dateChange(e) {
			this.model.occurrence = e.result;
		},
		scanCode() {
			uni.scanCode({
				scanType: ['barCode', 'qrCode'],
				success: res => {
					this.model.equipId = res.result;
				}
			});
		},
		bindConfirm() {
			if (this.model.equipId == undefined || this.model.equipId == '') {
				this.$u.toast('请选择设备');
				return;
			}
			if (this.model.trouble == undefined || this.model.trouble == '') {
				this.$u.toast('请输入问题描述');
				return;
			}
			this.model.status =1
			if (this.model.urls!=undefined){
				this.model.files = this.model.urls.join(',')
			}
			this.request({
				url: 'EamOrder/add',
				method: 'POST',
				data: this.model,
				success: res => {
					if (res.code == 200) {
						if (res.data == '1') {
							uni.navigateBack();
						}
					} else {
						this.$u.toast(res.msg);
					}
				}
			});
		}
	}
};
</script>

<style lang="scss">
	.landing{
		height: 84upx;
		line-height: 84upx;
		border-radius: 44upx;
		font-size: 32upx;
		background: linear-gradient(left,#FF978D, #FFBB69);
	}
</style>
